﻿package net.wangit.sms;

import net.wangit.Path;
import net.wangit.framework.exception.ConfigurationException;
import net.wangit.util.FileUtil;
import net.wangit.util.XMLUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;


public class SMSSettings {

	private static Log log;
	private static String path;
	private static String name;
	private static int baud;
	private static String port;
	private static String product;
	private static String model;

	public SMSSettings() {
	}

	public static String getModel() {
		return model;
	}

	public static String getName() {
		return name;
	}

	public static int getBaud() {
		return baud;
	}

	public static String getPort() {
		return port;
	}

	public static String getProduct() {
		return product;
	}

	static {
		log = LogFactory.getLog(net.wangit.sms.SMSSettings.class);
		path = (new StringBuilder(String.valueOf(Path.getHomePath()))).append(
				"/sms.xml").toString();
		try {
			if (!FileUtil.exists(path)) {
				log.error((new StringBuilder("sms file not found:")).append(
						path).toString());
				throw new ConfigurationException("sms file not found!");
			}
			Document document = XMLUtil.parseByPath(path);
			org.dom4j.Element root = document.getRootElement();
			name = XMLUtil.getTextString(root, "name", "");
			port = XMLUtil.getTextString(root, "port", "");
			baud = XMLUtil.getTextInt(root, "baud", -1);
			product = XMLUtil.getTextString(root, "product", "");
			model = XMLUtil.getTextString(root, "model", "");
			System.out.println((new StringBuilder("name=")).append(name)
					.toString());
			System.out.println((new StringBuilder("port=")).append(port)
					.toString());
			System.out.println((new StringBuilder("baud=")).append(baud)
					.toString());
			System.out.println((new StringBuilder("product=")).append(product)
					.toString());
			System.out.println((new StringBuilder("model=")).append(model)
					.toString());
		} catch (Exception e) {
			log.error("load sms settings failed!");
			throw new ConfigurationException(path, e);
		}
		log.info("load sms settings successfully.");
	}
}
